package com.huawang.mdesign.model.service.model.base;

import com.huawang.business.command.CommandAddType;
import com.huawang.business.command.CommandParameter;
import com.huawang.business.service.model.base.ElementFindService;
import com.huawang.business.service.model.base.ElementModelInfoService;
import com.huawang.business.service.model.base.ElementNameService;
import com.huawang.business.service.model.core.add.IModelOperation;
import com.huawang.business.service.model.core.add.ModelOperationFactory;
import com.huawang.core.definition.mof.instances.MetaClassInstance;
import com.huawang.core.semantics.constants.ModelPackageNameConstants;
import com.huawang.mdesign.model.service.constant.SpringBeanNameConstants;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import javax.validation.constraints.NotNull;

@Component(SpringBeanNameConstants.UML_SERVICE)
@Primary
public class UAFUmlService extends com.huawang.business.service.model.base.UmlService {
    @Autowired
    private ModelOperationFactory modelOperationFactory;

    @Autowired
    private ElementNameService elementNameService;

    @Autowired
    private ElementFindService elementFindService;

    @Autowired
    private ElementModelInfoService elementModelInfoService;

    /**
     * 根据metaClass和操作列表获得模型处理操作
     *
     * @param metaClass
     * @param operation
     * @return
     */
    @Override
    @Nullable
    public IModelOperation fetchModelOperation(String metaClass, String operation) {
        IModelOperation iModelOperation = modelOperationFactory.getOperation(ModelPackageNameConstants.UAF + metaClass);
        if(iModelOperation == null) {
            return super.fetchModelOperation(metaClass, operation);
        }
        return iModelOperation;
    }

    /**
     * 根据参数创建模型
     *
     * @param parameter
     * @return
     */
    @NotNull
    public MetaClassInstance createModel(@NotNull CommandParameter parameter) {
        String projectId = parameter.getProjectId();
        String name;
        //parameter 处理关系的parent
        if (parameter.getDefaultName() != null) {
            name = parameter.getDefaultName();
        } else if (StringUtils.isNotEmpty(parameter.getCustomName())) {
            name = this.elementNameService.fetchDefaultName(projectId, elementModelInfoService.fetchKey(parameter.getTypeId(), parameter.getApplyStereotypes(), parameter.getTypeName()), parameter.getParentId(), parameter.getCustomName(), parameter.getTypeName());
        } else if (CommandAddType.MODEL.equals(parameter.getType()) || CommandAddType.RELATIONSHIP.equals(parameter.getType()) && "AssociationClass".equals(parameter.getTypeId())) {
            name = this.elementNameService.fetchDefaultName(projectId, elementModelInfoService.fetchKey(parameter.getTypeId(), parameter.getApplyStereotypes(), parameter.getTypeName()), parameter.getParentId(), null, parameter.getTypeName());
        } else {
            name = "";
        }

        parameter.setDefaultName(name);

        this.addModel(parameter);
        return this.elementFindService.fetchInstance(parameter.getInstanceId(), projectId);
    }
}
